package com.sx.gongzhong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.DefendRoadListAdapter;
import com.sx.gongzhong.model.DefendRoadBean;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.DefendRoadListPest;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.IDefendRoadListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 全民护路
 */
public class DefendRoadActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener
        ,IDefendRoadListView{

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<DefendRoadBean> roadBeanList=new ArrayList<>();
    private DefendRoadListAdapter listAdapter;
    private int currentPager=1;
    private Disposable disposable;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.refresh_list);
        setTitleName(R.string.defend_road);
        setRightImage(R.mipmap.icon_nav_add);
        ButterKnife.bind(this);
        initView();
        initRx();
        onRefresh();
    }


    private void initRx() {
        disposable =  MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        if ("DefendRoad".equals(msgEvent.getRequest())) {
                            if (msgEvent.getType()== 1){
                                currentPager=1;
                                loadData();
                            }
                        }
                    }
                });
    }

    private void initView() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        listAdapter = new DefendRoadListAdapter(this,R.layout.item_defend_road_list,roadBeanList);
        listAdapter.setEmptyView(View.inflate(this,R.layout.load_error_view,null));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPager++;
                loadData();
            }
        },recyclerView);
        listAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(DefendRoadActivity.this,DefendRoadDetailActivity.class)
                .putExtra("protectionId",roadBeanList.get(position).getId()));
            }
        });
        findViewById(R.id.imv_Right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(DefendRoadActivity.this,DefendRoadAddActivity.class));
            }
        });
    }

    @Override
    public void onRefresh() {
       currentPager=1;
        loadData();
    }

    private void loadData() {
        swipeRefreshLayout.setRefreshing(true);
        new DefendRoadListPest(this).loadDefendRoadList(currentPager,10);
    }

    @Override
    public void showError(String message) {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void loadDefendRoadList(List<DefendRoadBean> data, int total) {
        swipeRefreshLayout.setRefreshing(false);
        if (currentPager == 1){
            roadBeanList.clear();
            listAdapter.setNewData(roadBeanList);
        }
        if (data != null && data.size() > 0) {
            roadBeanList.addAll(data);
        }
        if (roadBeanList.size() >= total){
            listAdapter.loadMoreEnd();
        }else {
            listAdapter.loadMoreComplete();
        }
        listAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        if (disposable!= null) {
            disposable = null;
        }
        super.onDestroy();
    }
}
